package com.longdaji.decoration.ui.activitiesOfMine.refund.forRefund;

import android.Manifest;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.longdaji.decoration.Constants;
import com.longdaji.decoration.R;
import com.longdaji.decoration.adapter.PhotoAdapter;
import com.longdaji.decoration.adapter.ProductAdapter;
import com.longdaji.decoration.base.BaseActivity;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.ui.activitiesOfMine.refund.refundDetail.RefundDetailActivity;
import com.longdaji.decoration.utils.BottomDialog;
import com.longdaji.decoration.utils.LogUtil;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import io.reactivex.functions.Consumer;

public class ForRefundActivity extends BaseActivity implements ForRefundContract.IView {

    @Inject
    ForRefundContract.IPresenter mPresenter;
    @BindView(R.id.tv_tool_title)
    TextView tvTitle;
    @BindView(R.id.tv_refund_reason)
    TextView tvRefundReason;
    @BindView(R.id.rv_photo)
    RecyclerView rvPhoto;
    @BindView(R.id.tv_refund_money)
    TextView tvRefundMoney;
    //    @BindView(R.id.rv_product)
//    RecyclerView rvProduct;
    @BindView(R.id.iv_product_img)
    ImageView ivProductImg;
    @BindView(R.id.tv_refund_name)
    TextView tvRefundName;
    @BindView(R.id.tv_refund_attr)
    TextView tvRefundAttr;
    @BindView(R.id.et_refund_description)
    EditText etRefundDescription;


    private BottomDialog mReasonDialog;
    private BottomDialog mPhotoDialog;

    private List<Uri> mUriList = new ArrayList<>();
    private List<Orders.OrdersBean.OrderBean.ProductsBean> productList = new ArrayList<>();
    private PhotoAdapter mPhotoAdapter;
    //    private ProductAdapter mProductAdapter;
    private static final int REQUEST_CODE_CHOOSE = 23;
    private String refundOrderNo;
    private int refundGoodsId;

    private Orders.OrdersBean.OrderBean.ProductsBean mProduct;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_for_refund);
        setUnBinder(ButterKnife.bind(this));

        if (mPresenter != null)
            mPresenter.setView(this);
        tvTitle.setText("申请退款");

        initView();
    }

    private void initView() {
        refundOrderNo = getIntent().getStringExtra(Constants.ORDER_NO);
        refundGoodsId = getIntent().getIntExtra(Constants.GOODS_ID, 0);

        rvPhoto.setLayoutManager(new GridLayoutManager(this, 4));
        mPhotoAdapter = new PhotoAdapter(mUriList);
        mPhotoAdapter.setAddPhotoListener(new PhotoAdapter.AddPhotoListener() {
            @Override
            public void addPhoto() {
                createPhotoDialog();
            }
        });
        rvPhoto.setAdapter(mPhotoAdapter);

//        mProductAdapter = new ProductAdapter(productList);
//        rvProduct.setLayoutManager(new LinearLayoutManager(this));
//        rvProduct.setAdapter(mProductAdapter);

        mPresenter.initData(refundOrderNo, refundGoodsId);
    }


    @Override
    public void onDestroy() {
        super.onDestroy();
        if (mPresenter != null)
            mPresenter.detachView();
    }

    @OnClick({R.id.rl_tool_back, R.id.tv_finish, R.id.rl_refund_reason})
    void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_tool_back:
                finish();
                break;
            case R.id.tv_finish:
                // TODO: 2018/4/10 0010 退款申请
                if (mProduct == null) {
                    LogUtil.e("未获取到退款商品");
                    return;
                }
                mPresenter.onFinishClick(refundOrderNo, mProduct.getSkuId(), mProduct.getProductNum(), mProduct.getGoodsPrice() * mProduct.getProductNum(),
                        tvRefundReason.getText().toString(), etRefundDescription.getText().toString().trim());
                break;
            case R.id.rl_refund_reason:
                createReasonDialog();
                break;
            default:
                break;
        }
    }

    private void createReasonDialog() {
        if (mReasonDialog == null) {
            mReasonDialog = new BottomDialog(this, "多拍，不想要", "拍错", "地址填错");
            mReasonDialog.setBottomDialogListener(new BottomDialog.BottomDialogListener() {
                @Override
                public void onItemOneClick() {
                    tvRefundReason.setText("多拍，不想要");
                }

                @Override
                public void onItemTwoClick() {
                    tvRefundReason.setText("拍错");
                }

                @Override
                public void onItemThreeClick() {
                    tvRefundReason.setText("地址填错");

                }
            });
        }
        mReasonDialog.show();
    }

    private void createPhotoDialog() {
        if (mPhotoDialog == null) {
            mPhotoDialog = new BottomDialog(this, "从手机相册选择");
            mPhotoDialog.setBottomDialogListener(new BottomDialog.BottomDialogListener() {
                @Override
                public void onItemOneClick() {
                    RxPermissions rxPermissions = new RxPermissions(ForRefundActivity.this);
                    rxPermissions.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
                            .subscribe(new Consumer<Boolean>() {
                                @Override
                                public void accept(Boolean aBoolean) throws Exception {
                                    if (aBoolean) {
                                        Log.d("mytest", "add photo");
                                        Matisse.from(ForRefundActivity.this)
                                                .choose(MimeType.allOf())
                                                .countable(true)
                                                .theme(R.style.Matisse_Dracula)
                                                .maxSelectable(12)
                                                .imageEngine(new GlideEngine())
                                                .forResult(REQUEST_CODE_CHOOSE);
                                    }
                                }
                            });
                }

                @Override
                public void onItemTwoClick() {

                }

                @Override
                public void onItemThreeClick() {

                }
            });
        }
        mPhotoDialog.show();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_CHOOSE && resultCode == RESULT_OK) {
            mUriList = Matisse.obtainResult(data);
            mPhotoAdapter.setData(mUriList);
        }
    }

    @Override
    public void showRefundSucceed() {
        // TODO: 2018/4/17 0017 提示退款申请成功并跳转到退款详情
        Intent intent = new Intent(ForRefundActivity.this, RefundDetailActivity.class);
        intent.putExtra(Constants.ORDER_NO, refundOrderNo);
        intent.putExtra(Constants.GOODS_ID, refundGoodsId);
        startActivity(intent);
        Toast.makeText(this, "申请退款成功", Toast.LENGTH_SHORT).show();
        finish();
    }

    @Override
    public void showForRefundOrderInfo(Orders.OrdersBean.OrderBean forRefundOrderInfo) {
//        mProductAdapter.setData(forRefundOrderInfo.getProducts());
//        tvRefundMoney.setText(forRefundOrderInfo.getOrderAmountTotal());
    }

    @Override
    public void showForRefundProductInfo(Orders.OrdersBean.OrderBean.ProductsBean product) {
        if (product == null) {
            LogUtil.e("mytest", "error: showForRefundProductInfo");
            return;
        }
        mProduct = product;
        tvRefundName.setText(mProduct.getGoodsName());
        tvRefundAttr.setText(mProduct.getGoodsAttr());
        Glide.with(this).load(mProduct.getGoodsSkuImg()).into(ivProductImg);
        tvRefundMoney.setText("退款金额￥" + String.valueOf(mProduct.getGoodsPrice() * mProduct.getProductNum()));
    }


}
